import QtQuick 1.1
import com.nokia.meego 1.0
import REngine 1.0
import "Common.js" as COM
RPage {
    id: bookmarkView

    property int longPressBookmarkId: 0
    property color backgroundColor: "white"
    property color textColor: "black"
    property string textFontFamily: "Arail"
    property string fileName: ""
    property int listItemHeight: 130

    function toFixed(value, precision) {
        var number = new Number(value)
        return number.toFixed(precision)
    }

    function reload() {
        console.log("reload")
        screen.allowedOrientations = engine.setting(REngine.Orientation)
        backgroundColor = engine.setting(REngine.TextBackgroud)
        textColor = engine.setting(REngine.TextColor)
        textFontFamily = engine.setting(REngine.TextFontFamiliy)

        //reload model
        listModel.clear()
        var bookmarkList = new Array()
        bookmarkList = engine.bookmarks(bookmarkView.fileName)
        console.log("bookmarks length", bookmarkList.length)
        for (var i = 0; i < bookmarkList.length; i++) {
            console.log("add bookmark:",
                        "id", bookmarkList[i].bookmarkId,
                        "lineNumber", bookmarkList[i].lineNum,
                        "charIndex", bookmarkList[i].charIndex,
                        "addTime", bookmarkList[i].addTime,
                        "note", bookmarkList[i].note)
            listModel.append({"bookmarkId": bookmarkList[i].bookmarkId,
                                 "lineNumber": bookmarkList[i].lineNum,
                                 "charIndex": bookmarkList[i].charIndex,
                                 "addTime": bookmarkList[i].addTime,
                                 "note": bookmarkList[i].note})
        }
    }

    function bookmarkCount() {
        if (listView.model != null) {
            console.log("model is not null, count", listView.model.count)
            return listView.model.count
        }
        else {
            console.log("model is null, return 0")
            return 0
        }
    }

    ListModel {
        id: listModel
    }

    Rectangle {
        anchors.fill: parent
        color: "black"

        Text {
            anchors.centerIn: parent
            text: qsTr("No Bookmark")
            color: "azure"
            font.family: "Arial"
            font.pixelSize: 60
            z: 1
            opacity: (bookmarkCount() > 0 ||
                      bookmarkView.status !== PageStatus.Active) ? 0 : 0.4
        }

        ListView {
            id: listView
            property int margin: 5
            anchors.fill: parent
            cacheBuffer: bookmarkView.listItemHeight*2
            model: listModel
            spacing: 5
            delegate: Thumbnail {
                fileName: bookmarkView.fileName
                width: listView.width - listView.margin
                height: bookmarkView.listItemHeight
                startLine: lineNumber
                startChar: charIndex
                viewFontSize: 18
                viewFontColor: bookmarkView.textColor
                viewFontFamily: bookmarkView.textFontFamily
                color: bookmarkView.backgroundColor
                titleText: note.length > 0 ? note : ""
                info1Text: engine.epocToString(addTime)
                info2Text: toFixed((lineNumber/engine.fileLineNumber(fileName))*100, 2)+"%"
                onClicked: {
                    console.log("bookmark ", bookmarkId, " is clicked, jump to it")
                    //save bookmark lineNum&charIndex as last position and move back to viewer
                    engine.saveLastPosition(fileName, lineNumber, charIndex)
                    bookmarkView.pageStack.pop()
                }
                onPressAndHold: {
                    console.log("file", model.fileName, " is longpressed, open context menu")
                    bookmarkView.longPressBookmarkId = bookmarkId
                    longPressMenu.open()
                }
            }
        }
        Component.onCompleted: {
            console.log("bookmarkView loaded, width", bookmarkView.width, "height", bookmarkView.height)
        }
    }
    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: {
                console.log("Back button clicked")
                bookmarkView.pageStack.pop()
            }
        }
        ToolIcon {
            iconId: "toolbar-view-menu"
            onClicked: {
                console.log("menu button clicked")
                optionsMenu.open()
            }
        }
    }
    onStatusChanged: {
        console.log("status changed:", status)
        if (status == PageStatus.Active) {
            console.log("reload model")
//            COM.disableDisplayBlank(true)
            reload()
        }
        else if (status == PageStatus.Activating) {
            screen.allowedOrientations = engine.setting(REngine.Orientation)
        }
        else if (status == PageStatus.Deactivating) {
//            COM.disableDisplayBlank(false)
        }
    }

    Connections {
        target: screen
        onCurrentOrientationChanged: {
            console.log("orientation Changed, reload model")
            reload()
        }
    }

    Connections {
        target: engine
        onBookmarkChanged: reload()
    }

//    Connections {
//        target: platformWindow
//        onViewModeChanged: {
//            console.log("viewModeChanged", platformWindow.viewMode)
//            COM.handleViewModeVisibleChanged()
//        }
//        onVisibleChanged: {
//            console.log("visibleChanged", platformWindow.visible)
//            COM.handleViewModeVisibleChanged()
//        }
//    }

//    Component.onCompleted: {
//        reload()
//    }

    ContextMenu {
        id: optionsMenu
        MenuLayout {
            MenuItem {
                text: qsTr("Remove All")
                onClicked: {
                    console.log("Remove menu clicked")
                    engine.removeAllBookmarks(bookmarkView.fileName)
                }
            }
        }
    }

    ContextMenu {
        id: longPressMenu
        MenuLayout {
            MenuItem {
                text: qsTr("Remove")
                onClicked: {
                    console.log("Remove menu clicked")
                    engine.removeBookmark(bookmarkView.longPressBookmarkId)
                }
            }
        }
    }
}
